layui.config({
    base: '/static/index/ext/'
}).extend({
    hashes: "hashes",
}).define(['layer','hashes'],function(exports){ //提示：模块也可以依赖其它模块，第一个参数是其他模块，第二个参数是回调
    let $ = layui.$,hashes=layui.hashes
    let obj = {
        encrypted: function(obj) {
            var newkey = Object.keys(obj).sort();
            var newObj = {}; //创建一个新的对象，用于存放排好序的键值对
            for (var i = 0; i < newkey.length; i++) { //遍历newkey数组
                newObj[newkey[i]] = obj[newkey[i]]; //向新创建的对象中按照排好的顺序依次增加键值对
            }
            var zhi = ''
            for (let i in newObj) {
                zhi += '&' + i + '=' + this.fixedEncodeURIComponent(newObj[i])
            }
            zhi = zhi.substr(1)
            newObj.app_key = hashes.md5(zhi + '197686741c09329446b110070f88f54d') //app_key 在这里
            return newObj; //返回排好序的新对象
        },
        fixedEncodeURIComponent: function  (str) {
            return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
                return '%' + c.charCodeAt(0).toString(16);
            });
        },
        //加密
        post: function(data,call,err){
            let li=window.location.pathname.split('/')[1],ur
            // if(li){
            //     ur= '/'+li+'/ajax.html?time='+Date.now()
            // }else{
            //    ur= 'ajax.html?time='+Date.now()
            // }
            ur=ur= '/index/ajax.html?time='+Date.now()
            data=this.encrypted(data);
            $.ajax({
                type: 'POST',
                url: ur,
                cache:false, //是否使用缓存
                dataType: "json",
                data: data,
                headers : {'token':'461f6924bed805723485e08b985049ebbb3a9774'},
                success: function(res,d){
                    if (d === "success") {
                        call(res)
                    }else{
                        err(res)
                    }
                },
                error: function(e) {
                    err(e)
                }
            });
        },
        mi:function (arr){
           return this.encrypted(arr);
        },
        json2get:function(json){
            var str = "";
            var arr =[];
            for(var key in json){
                arr.push(key + "=" + json[key]);
            }
            var len = arr.length;
            for(var i in arr){
                str += arr[i];
                if (i != (len-1)){
                    str += "&";
                }
            }
            return str;
        }
    };
    exports('lizhili', obj);
});